﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RelationExpertSystem;
using System.Diagnostics;

namespace RelationExpertSystem.Relation
{
    internal static class DependencyPrinter
    {
        private static HashSet<IRelation> _done;
        public static void PrintDependencies()
        {
            _done = new HashSet<IRelation>();
            PrintDependencies(Aunt.Instance, 0);
            PrintDependencies(Bastard.Instance, 0);
            PrintDependencies(Brother.Instance, 0);
            PrintDependencies(Brother_In_Law.Instance, 0);
            PrintDependencies(Child.Instance, 0);
            PrintDependencies(Child_In_Law.Instance, 0);
            PrintDependencies(Cousin.Instance, 0);
            PrintDependencies(Daughter.Instance, 0);
            PrintDependencies(Daughter_In_Law.Instance, 0);
            PrintDependencies(Ex_Spouse.Instance, 0);
            PrintDependencies(Father.Instance, 0);
            PrintDependencies(Father_In_Law.Instance, 0);
            PrintDependencies(Grandchild.Instance, 0);
            PrintDependencies(Granddaughter.Instance, 0);
            PrintDependencies(Grandfather.Instance, 0);
            PrintDependencies(Grandmother.Instance, 0);
            PrintDependencies(Grandparent.Instance, 0);
            PrintDependencies(Grandson.Instance, 0);
            PrintDependencies(Half_Brother.Instance, 0);
            PrintDependencies(Half_Sibling.Instance, 0);
            PrintDependencies(Half_Sister.Instance, 0);
            PrintDependencies(Husband.Instance, 0);
            PrintDependencies(Mother.Instance, 0);
            PrintDependencies(Mother_In_Law.Instance, 0);
            PrintDependencies(Nephew.Instance, 0);
            PrintDependencies(Niece.Instance, 0);
            PrintDependencies(Parent.Instance, 0);
            PrintDependencies(Parent_In_Law.Instance, 0);
            PrintDependencies(Self.Instance, 0);
            PrintDependencies(Sibling.Instance, 0);
            PrintDependencies(Sibling_In_Law.Instance, 0);
            PrintDependencies(Sister.Instance, 0);
            PrintDependencies(Sister_In_Law.Instance, 0);
            PrintDependencies(Son.Instance, 0);
            PrintDependencies(Son_In_Law.Instance, 0);
            PrintDependencies(Spouse.Instance, 0);
            PrintDependencies(Step_Brother.Instance, 0);
            PrintDependencies(Step_Child.Instance, 0);
            PrintDependencies(Step_Daughter.Instance, 0);
            PrintDependencies(Step_Father.Instance, 0);
            PrintDependencies(Step_Mother.Instance, 0);
            PrintDependencies(Step_Parent.Instance, 0);
            PrintDependencies(Step_Sibling.Instance, 0);
            PrintDependencies(Step_Sister.Instance, 0);
            PrintDependencies(Step_Son.Instance, 0);
            PrintDependencies(Uncle.Instance, 0);
            PrintDependencies(Wife.Instance, 0);
        }

        private static void PrintDependencies(IRelation relation, int level)
        {
            if (_done.Contains(relation))
                return;
            _done.Add(relation);

            string spacer = "";
            for (int j = 0; j < level; j++)
            {
                spacer += "  ";
            }

            Debug.WriteLine(String.Format("{0}{1}", spacer, relation.GetType().GetShortName()));

            bool hasDependencies = false;
            foreach (IRelation depencency in relation.GetDependencies())
            {
                hasDependencies = true;
                Debug.WriteLine(String.Format("{0}  relies on {1}", spacer, depencency.GetType().GetShortName()));
                PrintDependencies(depencency, ++level);
            }

            if (!hasDependencies)
                Debug.WriteLine(String.Format("{0}  relies on NOTHING", spacer));
        }
    }
}
